package framework 
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import joystick.JoyClient;
	import joystick.events.JoyClientEvent;
	import joystick.JoystickState;
	/**
	 * ...
	 * @author omega
	 */
	public class PadManager
	{
		private static var instance:PadManager = null;
		private var isInit:Boolean = false;
		private var keyPads:Array ;
		private var pads:Array;
		
		public static const KEYCODE_LEFTARROW:uint = 37;
		public static const KEYCODE_UPARROW:uint = 38;
		public static const KEYCODE_RIGHTARROW:uint = 39;
		public static const KEYCODE_DOWNARROW:uint = 40;
		public static const KEYCODE_Z:uint = 90;
		public static const KEYCODE_X:uint = 88;
		public static const KEYCODE_C:uint = 67;
		
		private var joystickStates:Array;
		private var joyClient:JoyClient;
		
		public function PadManager(caller:Function = null) {
			if (caller != PadManager.getInstance) {
				throw new Error("new禁止");
			}
		}
		
		public static function getInstance():PadManager {
			if (instance == null) {
				instance = new PadManager(arguments.callee);
			}
			return instance;
		}

		public function init(stage:Stage,player:uint = 1):void {
			if (isInit == true) return;
			
			joystickStates = new Array();
			keyPads = new Array();
			pads = new Array();
			for (var i:uint = 0; i < player; i++) {
				joystickStates.push(null);
				var k:KeyPad = new KeyPad(i);
				var p:Pad = new Pad();
				p.setKeyPad(k);
				keyPads.push(k);
				pads.push(p);
			}
						
			joyClient = new JoyClient();
			
			joyClient.addEventListener(JoyClientEvent.CONNECTED, onJoyClientConnected);
			joyClient.addEventListener(JoyClientEvent.IO_ERROR, onJoyClientError);
			joyClient.addEventListener(JoyClientEvent.SECURITY_ERROR, onJoyClientError);
			joyClient.addEventListener(JoyClientEvent.PACKET_RECEIVED, onJoyClientReceive);
			joyClient.addEventListener(JoyClientEvent.CLOSED, onJoyClientClose);
			
			joyClient.connect();

			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
		}
		
		public function beginFrame():void {
			for (var i:uint = 0; i < keyPads.length;i++){
//				joyClient.getState(i);
			}
		}
		
		public function endFrame():void {
			for (var i:uint = 0; i < keyPads.length;i++){
				keyPads[i].endFrame();
				joyClient.getState(i);
			}
		}
		
		private function onJoyClientConnected(e:JoyClientEvent):void {
			trace("joystick connected");
		}
		
		private function onJoyClientError(e:JoyClientEvent):void {
			trace("joystick error");
		}
		
		private function onJoyClientReceive(e:JoyClientEvent):void {
			if (e.state.deviceNumber < 0 || e.state.deviceNumber >= keyPads.length)return;
			var p:Pad = pads[e.state.deviceNumber];
			p.setJoystickState(e.state);
		}
		
		private function onJoyClientClose(e:JoyClientEvent):void {
			trace("joystick close");
		}
		
		private function onKeyDown(e:KeyboardEvent):void {
			for (var i:uint = 0; i < keyPads.length;i++){
				keyPads[i].onKeyDown(e.keyCode);
			}
		}
		
		private function onKeyUp(e:KeyboardEvent):void {
			for (var i:uint = 0; i < keyPads.length;i++){
				keyPads[i].onKeyUp(e.keyCode);
			}
		}
		
		public function getPad(player:uint):Pad {
			return pads[player];
		}
	}
}